// $Id$
/*
 * Copyright (C) 2011 Cedric Shih <cedric.shih@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef NIOCHANNEL_H_
#define NIOCHANNEL_H_

#ifdef __cplusplus
extern "C" {
#endif

struct niochannel;

struct niobuffer;

typedef struct niobuffer *(*niobuffer_create_cb)(
		struct niochannel *channel, void *arg);

typedef void (*niobuffer_release_cb)(struct niobuffer *buffer, void *arg);

struct niobuffer_factory {
	niobuffer_create_cb create;
	niobuffer_release_cb release;
};

struct nioreactor;

struct niochannel *niochannel_new(struct nioreactor *reactor,
		struct niobuffer_factory *factory, void *cbarg);

void niochannel_free(struct niochannel *channel);

struct niohandler;

int niochannel_add_first(struct niochannel *channel, const char *name,
		struct niohandler *handler, void *obj);

int niochannel_add_last(struct niochannel *channel, const char *name,
		struct niohandler *handler, void *obj);

int niochannel_add_before(struct niochannel *channel,
		const char *before, const char *name,
		struct niohandler *handler, void *obj);

int niochannel_add_after(struct niochannel *channel,
		const char *after, const char *name,
		struct niohandler *handler, void *obj);

typedef void (*niochannel_cb)(struct niochannel *channel, void *cbarg);

int niochannel_on_writable(struct niochannel *channel,
		niochannel_cb cb, void *cbarg);

int niochannel_write(struct niochannel *channel, void *message);

#ifdef __cplusplus
}
#endif

#endif /* NIOCHANNEL_H_ */
